package com.lagou.edu.utils;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@PropertySource(value = {"classpath:dataSource.properties"})    // 数据库配置文件
public class DataSourceConfig {

    @Value(value = "${dataSource.driver}")
    private String driverClass;
    @Value(value = "${dataSource.url}")
    private String jdbcUrl;
    @Value(value = "${dataSource.name}")
    private String username;
    @Value(value = "${dataSource.password}")
    private String password;

    @Bean(value = "dataSource")
    public DataSource createDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClass);
        druidDataSource.setUrl(jdbcUrl);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }
}
